package com.ghca.storm;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;

/**
 * Created by gh on 2016/10/24.
 */
public class SplitBolt extends BaseRichBolt {


    private Map stormConf;
    private TopologyContext context;
    private OutputCollector collector;
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
        this.stormConf = stormConf;
        this.context = context;
        this.collector = collector;
    }

    /**
     * 接收spout中的nextTuple，数据传输以Tuple为单位
     */
    public void execute(Tuple input) {
        // 接收每行数据
        String line = input.getStringByField("line");
        String[] words = line.split("\t");
        for (String word : words) {
            // 每行数据分隔，单词为单位发射
            this.collector.emit(new Values(word));
        }
    }

    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("word"));
    }
}
